<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <my-bread>评论管理</my-bread>
      </div>
      <el-table :data="form" style="width: 100%">
        <el-table-column
          prop="title"
          label="文章标题"
          width="500px"
        ></el-table-column>
        <el-table-column
          prop="total_comment_count"
          label="总评论数"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="fans_comment_count"
          label="粉丝评论数"
          width="180"
        >
        </el-table-column>
        <el-table-column prop="comment_status" label="评论状态" width="180">
          <template slot-scope="scope">
            {{ scope.row.comment_status ? "已打开" : "已关闭" }}
          </template>
        </el-table-column>
        <el-table-column prop="comment_status" label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="success"
              v-if="scope.row.comment_status"
              @click="toggleStatus(scope.row)"
              >关闭评论</el-button
            >
            <el-button
              size="small"
              @click="toggleStatus(scope.row)"
              type="danger"
              v-else
              >打开评论</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="reqParams.per_page"
        :current-page="reqParams.page"
        @current-change="changePage"
        style="margin-top: 20px"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Comment',
  data () {
    return {
      reqParams: {
        response_type: 'comment',
        page: 1,
        per_page: 20
      },

      form: [],
      total: null
    }
  },
  created () {
    this.getCommentList()
  },
  methods: {
    toggleStatus (row) {
      // console.log(row);

      const text1 =
        '此操作会关闭该文章的评论功能，关闭后用户将无法对该文章进行评论，您确认吗？'
      const text2 = '此操作会开启该文章的评论功能，您确认吗？'

      this.$confirm(row.comment_status ? text1 : text2, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            const {
              data: { data }
            } = await this.$http.put(
              '/mp/v1_0/comments/status?article_id=' + row.id.toString(),
              { allow_comment: !row.comment_status }
            )

            this.$message({
              type: 'success',
              message: data.allow_comment ? '打开评论' : '关闭评论成功'
            })
            row.comment_status = data.allow_comment
          } catch (e) {
            this.$message({
              type: 'error',
              message: '操作失败'
            })
          }
        })
        .catch(() => {})
    },
    changePage (changePage) {
      this.reqParams.page = changePage
      this.getCommentList()
    },
    async getCommentList () {
      const {
        data: { data }
      } = await this.$http.get('/mp/v1_0/articles', { params: this.reqParams })
      this.form = data.results
      this.total = data.total_count
    }
  }
}
</script>

<style>
</style>
